﻿#region << 版 本 注 释 >>
/*----------------------------------------------------------------
 * 版权所有 (c) 2022   保留所有权利。
 * CLR版本：4.0.30319.42000
 * 机器名称：DESKTOP-UL84BU5
 * 公司名称：
 * 命名空间：KingerRobot.WanliWMS.SpiService.Services.Warehouse
 * 唯一标识：12ebf505-a2fb-4260-8841-12f0df48f18f
 * 文件名：WarehouseManageImpl
 * 当前用户域：DESKTOP-UL84BU5
 * 
 * 创建者：zzl
 * 电子邮箱：1003590782@qq.com
 * 创建时间：2022/7/4 13:19:54
 * 版本：V1.0.0
 * 描述：
 *
 * ----------------------------------------------------------------
 * 修改人：
 * 时间：
 * 修改说明：
 *
 * 版本：V1.0.1
 *----------------------------------------------------------------*/
#endregion << 版 本 注 释 >>


using K8.MDCBB;
using KingerRobot.MexicoSailunMDC.Dao;
using KingerRobot.MexicoSailunMES.Spi;
using KingerRobot.MexicoSailunMES.Spi.DTO;
using KingerRobot.MexicoSailunWMS.Spi;
using Microsoft.EntityFrameworkCore.Metadata;
using System;
using System.Threading;
using System.Threading.Tasks;

namespace KingerRobot.MexicoSailunWMS.SpiService;
//WMS.Location
public class BLocationManageImpl : ILocationManageService
{

    private readonly static IRcsEquipWarehouse _RcsEquipWarehouse = new BRcsEquipWarehouse();
    private readonly static IWcsEquipLocHold _WcsEquipLocHold = new BWcsEquipLocHold();
    private readonly static IRcsLocation _RcsLocation = new BRcsLocation();
    private readonly static IRcsEquipCrn _RcsEquipCrn = new BRcsEquipCrn();
    private readonly static IProdMaterial _ProdMaterial = new BProdMaterial();
    private readonly static ITaskCmd _TaskCmd = new BTaskCmd();
    private readonly static ITask _Task = new BTask();
    private readonly static IWmsOutOrderSummary _WmsOutOrderSummary = new BWmsOutOrderSummary();
    private readonly static IWcsLocSku _WcsLocSku = new BWcsLocSku();
    private readonly static IProdProduct _ProdProduct = new BProdProduct();
    private readonly static IMesRpc _MesRpc = new BMesRpc();
    private readonly static IRcsEquip _RcsEquip = new BRcsEquip();
    private readonly static IWmsSetting _WmsSetting = new BWmsSetting();
    private readonly static IWms2MesService _Wms2MesService = new Wms2MesImpl();
    private readonly static IMesCuringRequestMaterial _MesCuringRequestMaterial = new BMesCuringRequestMaterial();
    private readonly static IMesProduct _MesProduct = new BMesProduct();
    private readonly static IProdSku _ProdSku = new BProdSku();

    [OperationLog("新增库存盘点")]
    public int AddMaterialLocation(BuildingLocation buildingLocation, long userId, string userName, string clientHost)
    {
        var barCode = buildingLocation?.BarCode;
        var tireInfo = _MesProduct.GetInfoByBarcode(barCode);
        var product = _MesRpc.GetMesProductBind(tireInfo);
        if (product is null)
        {
            return -1;
        }

        var result = 0;
        var materialId = product.POProdMaterial?.Id;
        var locCode = buildingLocation.LocId;
        var mdTime = product.POProdSku?.MdTime;
        var skuId = product.POProdSku?.Id;
        var location = _RcsLocation.GetLocationByCode(locCode);
        var locId = location?.Id ?? 0;
        var hasGood = location?.LocHasSku ?? 0;
        if (hasGood == 1)
        {
            return -2;
        }
        // 绑定位置
        result += _RcsLocation.UpdateLocationHasGood(materialId, locId, mdTime);
        if (result > 0)
        {
            result += _WcsLocSku.SetWcsLocSku(locId, skuId);
            //同步MES
            //SyncMes(skuId ?? 0, locId);
        }

        return result;
    }
    [OperationLog("库存盘点单删除")]
    public int DeleteMaterialLocation(long id, long userId, string userName, string clientHost)
    {
        // 查找任务
        var startLoc = _TaskCmd.GetTaskCmdByStartLocId(id);
        if (startLoc is not null && startLoc.Count > 0)
        {
            return -1;
        }
        var targetLoc = _TaskCmd.GetTaskCmdByTargetLocId(id);
        if (targetLoc is not null && targetLoc.Count > 0)
        {
            return -2;
        }

        var result = 0;
        // 清空库位
        result = _RcsLocation.ClearLocationHasTask(id);
        if (result > 0)
        {
            var skuId = _WcsLocSku.GetWcsLocSkuByLocId(id)?.SkuId ?? 0;
            result = _WcsLocSku.DeleteWcsLocSkuByLoc(id);
            //同步mes
            //SyncMesOnFinish(skuId);


            // 同步MES
            try
            {
                WriteLog($"sku{skuId}开始同步堆垛机出库mes", true);
                SyncMes(skuId);
            }
            catch (Exception ex)
            {
                WriteLog($"sku{skuId}同步堆垛机出库mes异常,{ex.ToString()}", true);
            }
        }

        return result;
    }

    public List<LocationBean> GetErrLocations(LocationPara locationPara)
    {
        var (equipLocs, locations, warehouses, equipCrns, wcsLocs, equips) = GetData();
        var orderSummaries = _WmsOutOrderSummary.GetOrderSummaryList();
        var tasks = _Task.GetTaskByType((int)EnumTaskType.Exwarehouse);

        var result = equipLocs.Join(locations, f => f.LocId, s => s.Id, (f, s) => new
        {
            EquipLocs = f,
            Locations = s,
        }).Join(equipCrns, f => f.EquipLocs.EquipId, s => s.Id, (f, s) => new
        {
            f.EquipLocs,
            f.Locations,
            EquipCrns = s,
        }).Join(warehouses, f => f.EquipCrns.WarehourseId, s => s.Id, (f, s) => new
        {
            f.EquipLocs,
            f.Locations,
            f.EquipCrns,
            Warehouses = s,
        }).GroupJoin(orderSummaries, f => f.Locations.Id, s => s.StartLoc, (f, s) => new
        {
            f.EquipLocs,
            f.Locations,
            f.EquipCrns,
            f.Warehouses,
            Summarys = s,
        }).SelectMany(t => t.Summarys.DefaultIfEmpty(), (f, s) => new
        {
            f.EquipLocs,
            f.Locations,
            f.EquipCrns,
            f.Warehouses,
            Summarys = s,
        }).GroupJoin(tasks, f => f.Locations.Id, s => s.StartLoc, (f, s) => new
        {
            f.EquipLocs,
            f.Locations,
            f.EquipCrns,
            f.Warehouses,
            f.Summarys,
            Tasks = s,
        }).SelectMany(t => t.Tasks.DefaultIfEmpty(), (f, s) => new
        {
            f.EquipLocs,
            f.Locations,
            f.EquipCrns,
            f.Warehouses,
            f.Summarys,
            Tasks = s,
        }).Where(t => t.Locations.LocEnable == 0 || t.Locations.InEnable == 0 || t.Locations.OutEnable == 0
        || t.Locations.ErrInHas > 0 || t.Locations.ErrOutEmpty > 0).ToList();

        if (!string.IsNullOrWhiteSpace(locationPara?.Name))
        {
            result = result.Where(t => t.Locations.LocName.Contains(locationPara.Name)).ToList();
        }
        if (locationPara?.LocEnable != null)
        {
            result = result.Where(t => t.Locations.LocEnable == locationPara.LocEnable).ToList();
        }
        if (locationPara?.WarehouseId != null)//指定仓库
        {
            result = result.Where(t => t.Warehouses.Id == locationPara?.WarehouseId).ToList();
        }

        if (locationPara?.InEnable != null)
        {
            result = result.Where(t => t.Locations.InEnable == locationPara.InEnable).ToList();
        }
        if (locationPara?.ErrInHas != null)
        {
            result = result.Where(t => t.Locations.ErrInHas == locationPara.ErrInHas).ToList();
        }

        if (locationPara?.OutEnable != null)
        {
            result = result.Where(t => t.Locations.OutEnable == locationPara.OutEnable).ToList();
        }

        if (locationPara?.ErrOutEmpty != null)
        {
            result = result.Where(t => t.Locations.ErrOutEmpty == locationPara.ErrOutEmpty).ToList();
        }
        // 获取物料信息
        var materialIds = result.Select(t => t.Locations.MaterialId).ToList();
        var materials = _ProdMaterial.GetMaterialListByIds(materialIds);


        return result.Select(t => new LocationBean
        {
            Id = t.Locations.Id.GetValueOrDefault(),
            Code = t.Locations.LocCode,
            Name = t.Locations.LocName,
            WarehouseName = t.Warehouses.WarehouseName,
            X = t.Locations.AxisX.GetValueOrDefault(),
            Y = t.Locations.AxisY.GetValueOrDefault(),
            Z = t.Locations.AxisZ.GetValueOrDefault(),
            T = t.Locations.AxisT.GetValueOrDefault(),
            LocHasSku = t.Locations.LocHasSku == 1 ? "有" : "无",
            LocHasSkuTime = t.Locations.LocHasSkuTime,
            LocEnable = t.Locations.LocEnable == 1 ? "可用" : "不可用",
            IsLocEnable = t.Locations.LocEnable == 1 ? true : false,
            InEnable = t.Locations.InEnable == 1 ? "可用" : "不可用",
            IsInEnable = t.Locations.InEnable == 1 ? true : false,
            ErrInHas = t.Locations.ErrInHas > 0 ? "是" : "否",

            OutEnable = t.Locations.OutEnable == 1 ? "可用" : "不可用",
            IsOutEnable = t.Locations.OutEnable == 1 ? true : false,
            ErrOutEmpty = t.Locations.ErrOutEmpty > 0 ? "是" : "否",

            Spec = materials.Where(m => m.Id == t.Locations.MaterialId).FirstOrDefault()?.Spec,

            IsOut = (t.Tasks is not null || t.Summarys is not null) ? 1 : 0,
            OutName = (t.Tasks is not null || t.Summarys is not null) ? $"是" : "否",

            TaskNo = t.Tasks?.TaskNo ?? 0,
        }).ToList();
    }

    public List<HasLocationBean> GetHasLocationsOld(HasLocationPara locationPara)
    {
        var datas = Task.Run(() =>
         {
             return GetData();
         });

        // var (equipLocs, locations, warehouses, equipCrns, wcsLocs, equips1) = GetData();
        var equipLocHolds = _WcsEquipLocHold.GetDistinctEquipLocHolds();
        var equips = _RcsEquip.GetEquips();
        var orderSummaries = _WmsOutOrderSummary.GetOrderSummaryList();
        var tasks = _Task.GetTaskByType((int)EnumTaskType.Exwarehouse);

        var curingMaterialGroups = _MesCuringRequestMaterial.GetCuringRequestMaterialGroup();

        //WriteLog($"硫化请料分组{curingMaterialGroups.ToJson()}", true);

        var (equipLocs, locations, warehouses, equipCrns, wcsLocs, equips1) = datas.Result;
        var result = equipLocHolds.Join(locations, f => f.LocId, s => s.Id, (f, s) => new
        {
            EquipLocs = f,
            Locations = s,
        }).Join(equipCrns, f => f.EquipLocs.EquipId, s => s.Id, (f, s) => new
        {
            f.EquipLocs,
            f.Locations,
            EquipCrns = s,
        }).Join(warehouses, f => f.EquipCrns.WarehourseId, s => s.Id, (f, s) => new
        {
            f.EquipLocs,
            f.Locations,
            f.EquipCrns,
            Warehouses = s,
        }).GroupJoin(orderSummaries, f => f.Locations.Id, s => s.StartLoc, (f, s) => new
        {
            f.EquipLocs,
            f.Locations,
            f.EquipCrns,
            f.Warehouses,
            Summarys = s,
        }).SelectMany(t => t.Summarys.DefaultIfEmpty(), (f, s) => new
        {
            f.EquipLocs,
            f.Locations,
            f.EquipCrns,
            f.Warehouses,
            Summarys = s,
        }).GroupJoin(tasks, f => f.Locations.Id, s => s.StartLoc, (f, s) => new
        {
            f.EquipLocs,
            f.Locations,
            f.EquipCrns,
            f.Warehouses,
            f.Summarys,
            Tasks = s,
        }).SelectMany(t => t.Tasks.DefaultIfEmpty(), (f, s) => new
        {
            f.EquipLocs,
            f.Locations,
            f.EquipCrns,
            f.Warehouses,
            f.Summarys,
            Tasks = s,
        }).Where(t => t.Locations.LocHasSku == 1).ToList();

        if (!string.IsNullOrWhiteSpace(locationPara?.Name))//指定货位名称
        {
            result = result.Where(t => t.Locations.LocName.Contains(locationPara.Name)).ToList();
        }

        if (locationPara?.WarehouseId != null)//指定仓库
        {
            result = result.Where(t => t.Warehouses.Id == locationPara?.WarehouseId).ToList();
        }

        if (!string.IsNullOrWhiteSpace(locationPara?.Code))//指定货位编号
        {
            result = result.Where(t => t.Locations.LocCode.Contains(locationPara.Code)).ToList();
        }

        if (locationPara?.LocEnable != null)//指定货位可用性
        {
            result = result.Where(t => t.Locations.LocEnable == locationPara.LocEnable).ToList();
        }

        if (locationPara?.InEnable != null)//指定入库可用
        {
            result = result.Where(t => t.Locations.InEnable == locationPara.InEnable).ToList();
        }

        if (locationPara?.OutEnable != null)//指定出库可用
        {
            result = result.Where(t => t.Locations.OutEnable == locationPara.OutEnable).ToList();
        }


        var locs = result.Select(t => t.Locations.Id).ToList();
        var wcsLocByLocs = wcsLocs.Where(t => locs.Contains(t.POWcsLocSku.LocId)).ToList();

        // 获取物料信息
        var materialIds = result.Select(t => t.Locations.MaterialId).ToList();
        var materials = _ProdMaterial.GetMaterialListByIds(materialIds);

        var data = result.Join(materials, f => f.Locations.MaterialId, s => s.Id, (f, s) => new
        {
            f.EquipLocs,
            f.Locations,
            f.EquipCrns,
            f.Warehouses,
            f.Summarys,
            f.Tasks,
            Materials = s,
        }).GroupJoin(curingMaterialGroups, f => f.Materials.Id, s => s.MaterialId, (f, s) => new
        {
            f.EquipLocs,
            f.Locations,
            f.EquipCrns,
            f.Warehouses,
            f.Summarys,
            f.Tasks,
            f.Materials,
            curingMaterialGroups = s
        }).SelectMany(t => t.curingMaterialGroups.DefaultIfEmpty(), (f, s) => new
        {
            f.EquipLocs,
            f.Locations,
            f.EquipCrns,
            f.Warehouses,
            f.Materials,
            f.Summarys,
            f.Tasks,
            CuringCode = s?.CuringCode
        }).
        GroupJoin(wcsLocByLocs, f => f.Locations.Id, s => s.POWcsLocSku.LocId, (f, s) => new
        {
            f.EquipLocs,
            f.Locations,
            f.EquipCrns,
            f.Warehouses,
            f.Materials,
            f.Summarys,
            f.Tasks,
            f.CuringCode,
            ProductInfo = s
        }).SelectMany(t => t.ProductInfo.DefaultIfEmpty(), (f, s) => new
        {
            f.EquipLocs,
            f.Locations,
            f.EquipCrns,
            f.Warehouses,
            f.Materials,
            f.Summarys,
            f.Tasks,
            f.CuringCode,
            Product = s?.POProdProduct
        }).OrderBy(t => t.Locations.MdTime).ToList();

        //.OrderBy(t => t.Product?.GtStatus).ThenBy(t => t.Locations.LocEnable).OrderByDescending(t => t.Product?.Frozen).ToList();

        if (!string.IsNullOrWhiteSpace(locationPara?.Spec))//指定规格
        {
            data = data.Where(t => t.Materials.Spec.Contains(locationPara.Spec)).ToList();
        }
        if (!string.IsNullOrWhiteSpace(locationPara?.MaterialCode))//指定物料编码
        {
            data = data.Where(t => t.Materials.MaterialCode.Contains(locationPara.MaterialCode)).ToList();
        }
        if (!string.IsNullOrWhiteSpace(locationPara?.Status))//指定状态
        {
            data = data.Where(t => t.Product is not null && t.Product.GtStatus == locationPara.Status).ToList();
        }
        if (!string.IsNullOrWhiteSpace(locationPara?.ProductCode))//指定条码
        {
            data = data.Where(t => t.Product is not null && t.Product.ProductCode.Contains(locationPara.ProductCode)).ToList();
        }
        //b c库暂时不允许人工出库
        if (locationPara.IsOut is not null && locationPara.IsOut == 1)
        {
            data = data.Where(t => t.Warehouses.Id == 1).ToList();
        }



        if (locationPara.Frozen is not null && locationPara.Frozen >= 0)//指定是否冻结
        {
            if (locationPara.Frozen == 0)
            {
                data = data.Where(t => t.Product is not null && t.Product.TireLock == 0).ToList();
            }
            else if (locationPara.Frozen == 1)
            {
                data = data.Where(t => t.Product is null || t.Product.TireLock == 1).ToList();
            }
        }

        var wmsSetting = _WmsSetting.GetWmsSettingByCode(ConstFields.ExWarehousingTime);
        _ = double.TryParse(wmsSetting?.Value, out var minute);


        var beans = data.Select(t => new HasLocationBean
        {
            Id = t.Locations.Id.GetValueOrDefault(),
            Code = t.Locations.LocCode,
            Name = t.Locations.LocName,
            WarehouseName = t.Warehouses.WarehouseName,
            LocHasSkuTime = t.Locations.LocHasSkuTime,
            MdTime = t.Locations.MdTime,
            OutTime = t.Product?.TbmExpirationTime,//t.Locations.MdTime?.AddMinutes(minute),
            IsTimeOut = t.Product?.TbmExpirationTime < DateTime.Now ? "已超期" : "未超期",
            LocEnable = t.Locations.LocEnable == 1 ? "否" : "是",//库存禁用
            InEnable = t.Locations.InEnable == 1 ? "可用" : "不可用",
            OutEnable = t.Locations.OutEnable == 1 ? "可用" : "不可用",
            ProductCode = t.Product?.ProductCode ?? "",
            Spec = t.Materials.Spec,
            MaterialCode = t.Materials.MaterialCode,
            MaterialName = t.Materials.MaterialName,
            GtStatus = t.Product?.GtStatus ?? "",
            GtStatusName = "0".Equals(t.Product?.GtStatus) ? "合格" : "不合格",
            EquipName = equips.Where(x => x.Id == t.EquipCrns.Id).FirstOrDefault()?.EquipName,
            EquipCode = equips.Where(x => x.Id == t.EquipCrns.Id).FirstOrDefault()?.Id,
            FrozenNum = t.Product is null ? 1 : t.Product.TireLock,
            FrozenStr = t.Product is null || t.Product.TireLock == 1 ? "已锁" : "未锁",

            IsOut = (t.Tasks is not null || t.Summarys is not null) ? 1 : 0,
            OutName = (t.Tasks is not null || t.Summarys is not null) ? "是" : "否",

            TaskNo = t.Tasks?.TaskNo ?? 0,
            CuringCode = t.CuringCode,
        }).Distinct().ToList();


        //if (!string.IsNullOrWhiteSpace(locationPara?.IsTimeOut))//是否超期
        //{
        //    var now = DateTime.Now;
        //    var outTime = (locationPara?.IsTimeOut == "1");

        //    if (outTime)
        //    {
        //        beans = beans.Where(t => t.OutTime < now).ToList();
        //    }
        //    else
        //    {
        //        beans = beans.Where(t => t.OutTime > now).ToList();
        //    }
        //}
        if (!string.IsNullOrWhiteSpace(locationPara?.IsTimeOut))//是否超期
        {
            if (locationPara?.IsTimeOut == "1")
            {
                beans = beans.Where(t => t.IsTimeOut == "已超期").ToList();
            }
            else if (locationPara?.IsTimeOut == "2")
            {
                beans = beans.Where(t => t.IsTimeOut == "未超期").ToList();
            }
            else
            {
                beans = beans.Where(t => t.IsTimeOut == locationPara?.IsTimeOut).ToList();
            }
        }


        if (!string.IsNullOrWhiteSpace(locationPara?.CrnName))//指定堆垛机名
        {
            beans = beans.Where(t => t.EquipName == locationPara?.CrnName).ToList();
        }

        if (!String.IsNullOrWhiteSpace(locationPara?.StockerCode) && Convert.ToInt64(locationPara?.StockerCode.Trim()) > 0)//指定堆垛机编号
        {
            beans = beans.Where(t => t.EquipCode == Convert.ToInt64(locationPara?.StockerCode.Trim())).ToList();
        }
        return beans;
    }


    public List<HasLocationBean> GetHasLocations(HasLocationPara locationPara)
    {

        var result = GetDataV2(locationPara);
        return result;

    }





    public List<LocationMaterialBean> GetLocationsMaterial(LocationMaterialPara locationMaterialPara)
    {

        var (equipLocs, locations, warehouses, equipCrns, wcsLocs, equips) = GetData();
        if (!string.IsNullOrWhiteSpace(locationMaterialPara.EquipId))
        {
            var equipHolds = _WcsEquipLocHold.GetEquipLocHolds().Where(t => t.EquipId == long.Parse(locationMaterialPara.EquipId)).Select(t => t.LocId).ToList();
            locations = locations.Where(t => equipHolds.Contains(t.Id)).ToList();
        }
        var result = equipLocs.Join(locations, f => f.LocId, s => s.Id, (f, s) => new
        {
            EquipLocs = f,
            Locations = s,
        }).Join(equipCrns, f => f.EquipLocs.EquipId, s => s.Id, (f, s) => new
        {
            f.EquipLocs,
            f.Locations,
            EquipCrns = s,
        }).Join(warehouses, f => f.EquipCrns.WarehourseId, s => s.Id, (f, s) => new
        {
            f.EquipLocs,
            f.Locations,
            f.EquipCrns,
            Warehouses = s,
        }).Join(equips, f => f.EquipCrns.Id, s => s.Id, (f, s) => new
        {
            f.EquipLocs,
            f.Locations,
            f.EquipCrns,
            f.Warehouses,
            Equips = s
        }).Where(t => t.Locations.LocHasSku == 1).ToList();


        if (locationMaterialPara?.WarehouseId != null && locationMaterialPara.WarehouseId != 0)
        {
            result = result.Where(t => t.Warehouses.Id == locationMaterialPara.WarehouseId).ToList();
        }

        if (!string.IsNullOrWhiteSpace(locationMaterialPara?.CrnName))
        {//根据堆垛机名称，指定堆垛机

            result = result.Where(t => t.Equips.EquipName.Contains(locationMaterialPara?.CrnName.Trim())).ToList();
        }


        // 获取物料信息
        var materialIds = result.Select(t => t.Locations.MaterialId).ToList();
        var materials = _ProdMaterial.GetMaterialListByIds(materialIds);

        var data = result.Join(materials, f => f.Locations.MaterialId, s => s.Id, (f, s) => new
        {
            f.EquipLocs,
            f.Locations,
            f.EquipCrns,
            f.Warehouses,
            Materials = s,
        }).ToList();

        if (!string.IsNullOrWhiteSpace(locationMaterialPara?.Spec))
        {
            data = data.Where(t => t.Materials.Spec.Contains(locationMaterialPara.Spec)).ToList();
        }
        if (!string.IsNullOrWhiteSpace(locationMaterialPara?.MaterialCode))
        {
            data = data.Where(t => t.Materials.MaterialCode.Contains(locationMaterialPara.MaterialCode)).ToList();
        }

        if (!string.IsNullOrWhiteSpace(locationMaterialPara?.MaterialName))
        {
            data = data.Where(t => t.Materials.MaterialName.Contains(locationMaterialPara.MaterialName)).ToList();
        }


        return data.Select(t => new LocationMaterialBean
        {
            Spec = t.Materials.Spec,
            MaterialCode = t.Materials.MaterialCode,
            MaterialName = t.Materials.MaterialName,
            WarehouseName = t.Warehouses.WarehouseName,
            MaterialCount = 1,
        }).GroupBy(t => new { t.Spec, t.MaterialCode, t.WarehouseName, t.MaterialName }).Select(t => new LocationMaterialBean
        {
            Spec = t.Key.Spec,
            MaterialCode = t.Key.MaterialCode,
            MaterialName = t.Key.MaterialName,
            WarehouseName = t.Key.WarehouseName,
            MaterialCount = t.Sum(s => s.MaterialCount),
        }).ToList();
    }
    public List<LocationBean> GetLocations(LocationPara locationPara)
    {

        var result = _RcsLocation.GetLocationV2();

        if (!string.IsNullOrWhiteSpace(locationPara?.Code))
        {
            result = result.Where(t => t.Code.Contains(locationPara.Code)).ToList();
        }
        if (!string.IsNullOrWhiteSpace(locationPara?.Name))
        {
            result = result.Where(t => t.Name.Contains(locationPara.Name)).ToList();
        }
        if (locationPara?.LocEnable != null)
        {
            result = result.Where(t => t.LocEnable == locationPara.LocEnable).ToList();
        }
        if (locationPara?.WarehouseId != null)//指定仓库
        {
            result = result.Where(t => t.WarehouseId == locationPara?.WarehouseId).ToList();
        }
        if (locationPara?.InEnable != null)
        {
            result = result.Where(t => t.InEnable == locationPara.InEnable).ToList();
        }
        if (locationPara?.ErrInHas != null)
        {
            result = result.Where(t => t.ErrInHas == locationPara.ErrInHas).ToList();
        }

        if (locationPara?.OutEnable != null)
        {
            result = result.Where(t => t.OutEnable == locationPara.OutEnable).ToList();
        }

        if (locationPara?.ErrOutEmpty != null)
        {
            result = result.Where(t => t.ErrOutEmpty == locationPara.ErrOutEmpty).ToList();
        }
        if (locationPara?.EquipName != null && !string.IsNullOrWhiteSpace(locationPara.EquipName))
        {
            result = result.Where(t => t.EquipName == locationPara.EquipName).ToList();
        }

        // 获取物料信息
        //var materialIds = result.Select(t => t.Locations.MaterialId).ToList();
        //var materials = _ProdMaterial.GetMaterialListByIds(materialIds);


        return result.Select(t => new LocationBean
        {
            Id = t.Id,
            Code = t.Code,
            Name = t.Name,
            WarehouseName = t.WarehouseName,
            X = t.X,
            Y = t.Y,
            Z = t.Z,
            T = t.T,
            LocHasSku = t.LocHasSku == 1 ? "有" : "无",
            LocHasSkuTime = t.LocHasSkuTime,
            LocEnable = t.LocEnable == 1 ? "可用" : "不可用",
            IsLocEnable = t.LocEnable == 1 ? true : false,
            InEnable = t.InEnable == 1 ? "可用" : "不可用",
            IsInEnable = t.InEnable == 1 ? true : false,
            ErrInHas = t.ErrInHas > 0 ? "是" : "否",
            LocType = t.LocType,
            OutEnable = t.OutEnable == 1 ? "可用" : "不可用",
            IsOutEnable = t.OutEnable == 1 ? true : false,
            ErrOutEmpty = t.ErrOutEmpty > 0 ? "是" : "否",

            RelateScore = t.RelateScore,


            Spec = t.Spec,
            Remarks = t.Remarks, //禁用信息,
            EquipName = t.EquipName,
        }).OrderBy(t => t.Id).ThenBy(t => t.RelateScore).ToList();
    }


    public List<HasLocationBean> GetTimeOutLocations(HasLocationPara locationPara)
    {

        var (equipLocs, locations, warehouses, equipCrns, wcsLocs, equips) = GetData();

        var result = equipLocs.Join(locations, f => f.LocId, s => s.Id, (f, s) => new
        {
            EquipLocs = f,
            Locations = s,
        }).Join(equipCrns, f => f.EquipLocs.EquipId, s => s.Id, (f, s) => new
        {
            f.EquipLocs,
            f.Locations,
            EquipCrns = s,
        })
        .Join(equips, f => f.EquipCrns.Id, s => s.Id, (f, s) => new
        {
            f.EquipLocs,
            f.Locations,
            f.EquipCrns,
            Equip = s
        })
        .Join(warehouses, f => f.EquipCrns.WarehourseId, s => s.Id, (f, s) => new
        {
            f.EquipLocs,
            f.Locations,
            f.EquipCrns,
            f.Equip,
            Warehouses = s,
        }).Where(t => t.Locations.LocHasSku == 1).ToList();

        if (!string.IsNullOrWhiteSpace(locationPara?.Name))
        {
            result = result.Where(t => t.Locations.LocName.Contains(locationPara.Name)).ToList();
        }

        if (!string.IsNullOrWhiteSpace(locationPara?.Code))
        {
            result = result.Where(t => t.Locations.LocCode.Contains(locationPara.Code)).ToList();
        }

        if (locationPara?.LocEnable != null)
        {
            result = result.Where(t => t.Locations.LocEnable == locationPara.LocEnable).ToList();
        }

        if (locationPara?.InEnable != null)
        {
            result = result.Where(t => t.Locations.InEnable == locationPara.InEnable).ToList();
        }

        if (locationPara?.OutEnable != null)
        {
            result = result.Where(t => t.Locations.OutEnable == locationPara.OutEnable).ToList();
        }


        // 获取物料信息
        var materialIds = result.Select(t => t.Locations.MaterialId).ToList();
        var materials = _ProdMaterial.GetMaterialListByIds(materialIds);

        var data = result.Join(materials, f => f.Locations.MaterialId, s => s.Id, (f, s) => new
        {
            f.EquipLocs,
            f.Locations,
            f.EquipCrns,
            f.Equip,
            f.Warehouses,
            Materials = s,
        }).Join(wcsLocs, f => f.Locations.Id, s => s.POWcsLocSku.LocId, (f, s) => new
        {
            f.EquipLocs,
            f.Locations,
            f.EquipCrns,
            f.Equip,
            f.Warehouses,
            f.Materials,
            Product = s.POProdProduct,
        }).OrderByDescending(t => t.Product?.Frozen).ToList();
        if (!string.IsNullOrWhiteSpace(locationPara?.Spec))
        {
            data = data.Where(t => t.Materials.Spec.Contains(locationPara.Spec)).ToList();
        }
        if (!string.IsNullOrWhiteSpace(locationPara?.MaterialCode))
        {
            data = data.Where(t => t.Materials.MaterialCode.Contains(locationPara.MaterialCode)).ToList();
        }
        if (!string.IsNullOrWhiteSpace(locationPara?.Status))
        {
            data = data.Where(t => t.Product.GtStatus == locationPara.Status).ToList();
        }
        if (!string.IsNullOrWhiteSpace(locationPara?.ProductCode))
        {
            data = data.Where(t => t.Product.ProductCode.Contains(locationPara.ProductCode)).ToList();
        }
        if (!string.IsNullOrWhiteSpace(locationPara?.CrnName))
        {
            data = data.Where(t => t.Equip.EquipName == locationPara.CrnName).ToList();
        }

        var wmsSetting = _WmsSetting.GetWmsSettingByCode(ConstFields.ExWarehousingTime);
        _ = double.TryParse(wmsSetting?.Value, out var minute);

        var resultData = data.Select(t => new HasLocationBean
        {
            Id = t.Locations.Id.GetValueOrDefault(),
            Code = t.Locations.LocCode,
            Name = t.Locations.LocName,
            WarehouseName = t.Warehouses.WarehouseName,
            LocHasSkuTime = t.Locations.LocHasSkuTime,
            MdTime = t.Locations.MdTime,
            OutTime = t.Locations.MdTime?.AddMinutes(minute),
            LocEnable = t.Locations.LocEnable == 1 ? "否" : "是",//库位禁用
            InEnable = t.Locations.InEnable == 1 ? "可用" : "不可用",
            OutEnable = t.Locations.OutEnable == 1 ? "可用" : "不可用",
            ProductCode = t.Product?.ProductCode,
            Spec = t.Materials.Spec,
            MaterialCode = t.Materials.MaterialCode,
            MaterialName = t.Materials.MaterialName,
            GtStatus = t.Product?.GtStatus,
            GtStatusName = "0".Equals(t.Product?.GtStatus) ? "合格" : "不合格",
            FrozenNum = t.Product is null ? 1 : t.Product.TireLock,
            FrozenStr = t.Product is null || t.Product.TireLock == 1 ? "锁" : "未锁",

        }).ToList();

        var nowDateTime = DateTime.Now;

        return resultData.Where(t => t.OutTime < nowDateTime).ToList();
    }
    [OperationLog("库位入库启用禁用")]
    public int UpdateInEnable(long id, long userId, string userName, string clientHost)
    {
        var result = _RcsLocation.UpdateInEnable(id);
        return result;
    }

    public int UpdateLocEnable(long id)
    {
        var result = _RcsLocation.UpdateLocEnable(id);
        return result;
    }

    [OperationLog("库位启用禁用")]
    public int ChangeLocEnable(LocEnableParam param, long userId, string userName, string clientHost)
    {
        var result = _RcsLocation.ChangeLocEnable(param);
        return result;
    }

    [OperationLog("库位出库启用禁用")]
    public int UpdateOutEnable(long id, long userId, string userName, string clientHost)
    {
        var result = _RcsLocation.UpdateOutEnable(id);
        return result;
    }

    public List<HasLocationBean> GetLocationsNoMaterial(LocationNoMaterialPara locationNoMaterialPara)
    {

        var (equipLocs, locations, warehouses, equipCrns, wcsLocs, equips) = GetData();

        var result = equipLocs.Join(locations, f => f.LocId, s => s.Id, (f, s) => new
        {
            EquipLocs = f,
            Locations = s,
        }).Join(equipCrns, f => f.EquipLocs.EquipId, s => s.Id, (f, s) => new
        {
            f.EquipLocs,
            f.Locations,
            EquipCrns = s,
        }).Join(warehouses, f => f.EquipCrns.WarehourseId, s => s.Id, (f, s) => new
        {
            f.EquipLocs,
            f.Locations,
            f.EquipCrns,
            Warehouses = s,
        }).Where(t => t.Locations.LocHasSku == 1 && t.Locations.MaterialId == 1).ToList();

        if (!string.IsNullOrWhiteSpace(locationNoMaterialPara?.Name))
        {
            result = result.Where(t => t.Locations.LocName.Contains(locationNoMaterialPara.Name)).ToList();
        }

        if (locationNoMaterialPara?.LocEnable != null)
        {
            result = result.Where(t => t.Locations.LocEnable == locationNoMaterialPara.LocEnable).ToList();
        }

        if (locationNoMaterialPara?.InEnable != null)
        {
            result = result.Where(t => t.Locations.InEnable == locationNoMaterialPara.InEnable).ToList();
        }

        if (locationNoMaterialPara?.OutEnable != null)
        {
            result = result.Where(t => t.Locations.OutEnable == locationNoMaterialPara.OutEnable).ToList();
        }


        // 获取物料信息
        var materialIds = result.Select(t => t.Locations.MaterialId).ToList();
        var materials = _ProdMaterial.GetMaterialListByIds(materialIds);

        var data = result.Join(materials, f => f.Locations.MaterialId, s => s.Id, (f, s) => new
        {
            f.EquipLocs,
            f.Locations,
            f.EquipCrns,
            f.Warehouses,
            Materials = s,
        }).Join(wcsLocs, f => f.Locations.Id, s => s.POWcsLocSku.LocId, (f, s) => new
        {
            f.EquipLocs,
            f.Locations,
            f.EquipCrns,
            f.Warehouses,
            f.Materials,
            Product = s.POProdProduct,
        }).ToList();

        var wmsSetting = _WmsSetting.GetWmsSettingByCode(ConstFields.ExWarehousingTime);
        _ = double.TryParse(wmsSetting?.Value, out var minute);

        return data.Select(t => new HasLocationBean
        {
            Id = t.Locations.Id.GetValueOrDefault(),
            Code = t.Locations.LocCode,
            Name = t.Locations.LocName,
            WarehouseName = t.Warehouses.WarehouseName,
            LocHasSkuTime = t.Locations.LocHasSkuTime,
            MdTime = t.Locations.MdTime,
            OutTime = t.Locations.MdTime?.AddMinutes(minute),
            LocEnable = t.Locations.LocEnable == 1 ? "可用" : "不可用",
            InEnable = t.Locations.InEnable == 1 ? "可用" : "不可用",
            OutEnable = t.Locations.OutEnable == 1 ? "可用" : "不可用",
            ProductCode = t.Product.ProductCode,
            Spec = t.Materials.Spec,
            MaterialCode = t.Materials.MaterialCode,
            MaterialName = t.Materials.MaterialName,
        }).ToList();
    }

    private (List<POWcsEquipLocHold>, List<PORcsLocation>, List<PORcsEquipWarehouse>, List<PORcsEquipCrn>, List<WcsLocSkuBean>, List<PORcsEquip>) GetData()
    {
        var equipLocs = _WcsEquipLocHold.GetEquipLocHolds(); // 立库货位
        var locations = _RcsLocation.GetLocations(); // 所有货位
        var warehouses = _RcsEquipWarehouse.GetPORcsEquipWarehouses(); // 仓库
        var equipCrns = _RcsEquipCrn.GetRcsEquipCrn(); // 堆垛机
        var wcsLocs = _WcsLocSku.GetWcsLocSkuBeans(); // 产品
                                                      //_MesCuringRequestMaterial.GetCuringRequestMaterialByMaterialId();


        List<PORcsEquip> Equips = null;// _RcsEquipCrn.GetBRcsEquips();//设备

        return (equipLocs, locations, warehouses, equipCrns, wcsLocs, Equips);
    }
    private List<HasLocationBean> GetDataV2(HasLocationPara locationPara)
    {


        var db = Db.Default;

        var data = db.WcsEquipLocHolds.Join(db.RcsLocations, f => f.LocId, s => s.Id, (f, s) => new
        {
            EquipLocs = f,
            Locations = s,
        }).Join(db.RcsEquipCrns, f => f.EquipLocs.EquipId, s => s.Id, (f, s) => new
        {
            f.EquipLocs,
            f.Locations,
            EquipCrns = s,
        }).Join(db.RcsEquips, f => f.EquipCrns.Id, s => s.Id, (f, s) => new
        {

            f.EquipLocs,
            f.Locations,
            f.EquipCrns,
            RcsEquips = s,
        }).Join(db.WcsLocSkus, f => f.Locations.Id, s => s.LocId, (f, s) => new
        {

            f.EquipLocs,
            f.Locations,
            f.EquipCrns,
            f.RcsEquips,
            s.SkuId,
        }).Join(db.ProdSkuProducts, f => f.SkuId, s => s.SkuId, (f, s) => new
        {

            f.EquipLocs,
            f.Locations,
            f.EquipCrns,
            f.RcsEquips,
            f.SkuId,
            s.ProductId,
        }).Join(db.ProdProducts, f => f.ProductId, s => s.Id, (f, s) => new
        {

            f.EquipLocs,
            f.Locations,
            f.EquipCrns,
            f.RcsEquips,
            f.SkuId,
            ProdProducts = s,
        }).Join(db.MesProducts, f => f.ProdProducts.MesId, s => s.Id, (f, s) => new
        {

            f.EquipLocs,
            f.Locations,
            f.EquipCrns,
            f.RcsEquips,
            f.SkuId,
            f.ProdProducts,
            s.TbmNo
        }).Join(db.ProdMaterials, f => f.ProdProducts.MaterialId, s => s.Id, (f, s) => new
        {

            f.EquipLocs,
            f.Locations,
            f.EquipCrns,
            f.RcsEquips,
            f.SkuId,
            f.ProdProducts,
            f.TbmNo,
            ProdMaterials = s,
        })
        .Join(db.RcsEquipWarehouses, f => f.EquipCrns.WarehourseId, s => s.Id, (f, s) => new
        {
            f.EquipLocs,
            f.Locations,
            f.EquipCrns,
            f.RcsEquips,
            f.SkuId,
            f.ProdProducts,
            f.TbmNo,
            f.ProdMaterials,
            Warehouses = s,
        }).GroupJoin(db.WmsOutOrderSummarys, f => f.Locations.Id, s => s.StartLoc, (f, s) => new
        {
            f.EquipLocs,
            f.Locations,
            f.EquipCrns,
            f.RcsEquips,
            f.SkuId,
            f.ProdProducts,
            f.TbmNo,
            f.ProdMaterials,
            f.Warehouses,
            Summarys = s,
        }).SelectMany(t => t.Summarys.DefaultIfEmpty(), (f, s) => new
        {
            f.EquipLocs,
            f.Locations,
            f.EquipCrns,
            f.RcsEquips,
            f.SkuId,
            f.ProdProducts,
            f.TbmNo,
            f.ProdMaterials,
            f.Warehouses,
            Summarys = s,
        }).GroupJoin(db.WcsTasks, f => f.Locations.Id, s => s.StartLoc, (f, s) => new
        {
            f.EquipLocs,
            f.Locations,
            f.EquipCrns,
            f.RcsEquips,
            f.SkuId,
            f.ProdProducts,
            f.TbmNo,
            f.ProdMaterials,
            f.Warehouses,
            f.Summarys,
            Tasks = s,
        }).SelectMany(t => t.Tasks.DefaultIfEmpty(), (f, s) => new
        {
            f.EquipLocs,
            f.Locations,
            f.EquipCrns,
            f.RcsEquips,
            f.SkuId,
            f.ProdProducts,
            f.TbmNo,
            f.ProdMaterials,
            f.Warehouses,
            f.Summarys,
            Tasks = s,
        }).Where(t => t.Locations.LocHasSku == 1 && t.Locations.LocType == (int)EnumLocType.Location);



        if (!string.IsNullOrWhiteSpace(locationPara?.Name))//指定货位名称
        {
            data = data.Where(t => t.Warehouses.WarehouseName.Contains(locationPara.Name));
        }

        if (locationPara is not null && locationPara?.WarehouseId != null)//指定仓库
        {
            data = data.Where(t => t.Warehouses.Id == locationPara.WarehouseId);
        }

        if (!string.IsNullOrWhiteSpace(locationPara?.Code))//指定货位编号
        {
            data = data.Where(t => t.Locations.LocCode.Contains(locationPara.Code));
        }

        if (locationPara is not null && locationPara?.LocEnable != null)//指定货位可用性
        {
            data = data.Where(t => t.Locations.LocEnable == locationPara.LocEnable);
        }

        if (locationPara is not null && locationPara?.InEnable != null)//指定入库可用
        {
            data = data.Where(t => t.Locations.InEnable == locationPara.InEnable);
        }

        if (locationPara is not null && locationPara?.OutEnable != null)//指定出库可用
        {
            data = data.Where(t => t.Locations.OutEnable == locationPara.OutEnable);
        }


        if (!string.IsNullOrWhiteSpace(locationPara?.Spec))//指定规格
        {
            data = data.Where(t => t.ProdMaterials.Spec.Contains(locationPara.Spec));
        }
        if (!string.IsNullOrWhiteSpace(locationPara?.MaterialCode))//指定物料编码
        {
            data = data.Where(t => t.ProdMaterials.MaterialCode.Contains(locationPara.MaterialCode));
        }
        if (!string.IsNullOrWhiteSpace(locationPara?.Status))//指定状态
        {
            data = data.Where(t => t.ProdProducts != null && t.ProdProducts.GtStatus == locationPara.Status);
        }
        if (!string.IsNullOrWhiteSpace(locationPara?.ProductCode))//指定条码
        {
            data = data.Where(t => t.ProdProducts != null && t.ProdProducts.ProductCode.Contains(locationPara.ProductCode));
        }
        // c库暂时不允许人工出库
       //if (locationPara is not null && locationPara.IsOut is not null && locationPara.IsOut == 1)
       // {
       //     data = data.Where(t => t.Warehouses.Id == 1 || t.Warehouses.Id == 2);
       // }



        if (locationPara is not null && locationPara.Frozen is not null && locationPara.Frozen >= 0)//指定是否冻结
        {
            if (locationPara.Frozen == 0)
            {
                data = data.Where(t => t.ProdProducts != null && t.ProdProducts.TireLock == 0);
            }
            else if (locationPara.Frozen == 1)
            {
                data = data.Where(t => t.ProdProducts != null && t.ProdProducts.TireLock == 1);
            }
        }

        if (!string.IsNullOrWhiteSpace(locationPara?.CrnName))//指定堆垛机名
        {
            data = data.Where(t => t.RcsEquips.EquipName == locationPara.CrnName);
        }

        //if (!String.IsNullOrWhiteSpace(locationPara?.StockerCode) && Convert.ToInt64(locationPara?.StockerCode.Trim()) > 0)//指定堆垛机编号
        //{
        //    data = data.Where(t => t.RcsEquips.EquipCode == Convert.ToInt64(locationPara.StockerCode.Trim()));
        //}


        var result = data.Select(t => new HasLocationBean
        {
            Id = t.Locations.Id.GetValueOrDefault(),
            Code = t.Locations.LocCode,
            Name = t.Locations.LocName,
            WarehouseName = t.Warehouses.WarehouseName,
            WarehouseId = t.Warehouses.Id ?? 0,
            LocHasSkuTime = t.Locations.LocHasSkuTime,
            MdTime = t.Locations.MdTime,
            OutTime = t.ProdProducts.TbmExpirationTime,//t.Locations.MdTime?.AddMinutes(minute),
            IsTimeOut = t.ProdProducts.TbmExpirationTime < DateTime.Now ? "已超期" : "未超期",
            LocEnable = t.Locations.LocEnable == 1 ? "否" : "是",//库存禁用
            InEnable = t.Locations.InEnable == 1 ? "可用" : "不可用",
            OutEnable = t.Locations.OutEnable == 1 ? "可用" : "不可用",
            ProductCode = t.ProdProducts.ProductCode ?? "",
            Spec = t.ProdMaterials.Spec,
            MaterialCode = t.ProdMaterials.MaterialCode,
            MaterialName = t.ProdMaterials.MaterialName,
            GtStatus = t.ProdProducts.GtStatus ?? "",
            GtStatusName = "0".Equals(t.ProdProducts.GtStatus) ? "合格" : "不合格",
            EquipName = t.RcsEquips.EquipName,
            TbmNo = t.TbmNo,
            EquipCode = t.RcsEquips.Id,
            FrozenNum = t.ProdProducts.TireLock,
            FrozenStr = t.ProdProducts.TireLock == 1 ? "已锁" : "未锁",

            IsOut = (t.Tasks == null || t.Summarys == null) ? 0 : 1,
            OutName = (t.Tasks == null || t.Summarys == null) ? "否" : "是",

            TaskNo = t.Tasks == null ? 0 : t.Tasks.TaskNo ?? 0,
            //CuringCode = t.CuringCode,
        }).Distinct().OrderBy(t => t.MdTime).ToList();

        if (!string.IsNullOrWhiteSpace(locationPara?.IsTimeOut))//超期
        {
            result = result.Where(t => t.IsTimeOut == locationPara.IsTimeOut).ToList();
        }



        return result;


    }




    public List<LocationBean> SetLocations(LocationPara locationPara)
    {
        var db = Db.Default;
        List<PORcsLocation> result = new();



        //var equipLocs = _WcsEquipLocHold.GetEquipLocHolds(); // 立库货位
        //var locations = _RcsLocation.GetLocations(); // 所有货位
        //var warehouses = _RcsEquipWarehouse.GetPORcsEquipWarehouses(); // 仓库
        //var equipCrns = _RcsEquipCrn.GetRcsEquipCrn(); // 堆垛机



        if (!string.IsNullOrWhiteSpace(locationPara?.locationType))
        {
            result = _RcsLocation.GetLocationByType(Convert.ToInt32(locationPara?.locationType));
        }
        else
        {
            var locLocation = _RcsLocation.GetLocationByType((int)EnumLocType.Location);
            var locEms = _RcsLocation.GetLocationByType((int)EnumLocType.EMSInPut);
            var locInPut = _RcsLocation.GetLocationByType((int)EnumLocType.InPut);
            var locOutPut = _RcsLocation.GetLocationByType((int)EnumLocType.OutPut);
            //result = locLocation.Union(locOutPut).Union(locInPut);

            result.AddRange(locLocation);
            result.AddRange(locOutPut);
            result.AddRange(locInPut);
            result.AddRange(locEms);
        }

        if (!string.IsNullOrWhiteSpace(locationPara?.Code))
        {
            result = result.Where(t => t.LocCode.Contains(locationPara.Code)).ToList();
        }
        if (!string.IsNullOrWhiteSpace(locationPara?.Name))
        {
            result = result.Where(t => t.LocName.Contains(locationPara.Name)).ToList();
        }
        //if (locationPara?.LocEnable != null)
        //{
        //    result = result.Where(t => t.LocEnable == locationPara.LocEnable).ToList();
        //}

        //if (locationPara?.InEnable != null)
        //{
        //    result = result.Where(t => t.InEnable == locationPara.InEnable).ToList();
        //}
        //if (locationPara?.ErrInHas != null)
        //{
        //    result = result.Where(t => t.ErrInHas == locationPara.ErrInHas).ToList();
        //}

        //if (locationPara?.OutEnable != null)
        //{
        //    result = result.Where(t => t.OutEnable == locationPara.OutEnable).ToList();
        //}

        //if (locationPara?.ErrOutEmpty != null)
        //{
        //    result = result.Where(t => t.ErrOutEmpty == locationPara.ErrOutEmpty).ToList();
        //}
        // 获取物料信息
        //var materialIds = result.Select(t => t.Locations.MaterialId).ToList();
        //var materials = _ProdMaterial.GetMaterialListByIds(materialIds);


        return result.Select(t => new LocationBean
        {
            Id = t.Id.GetValueOrDefault(),
            Code = t.LocCode,
            Name = t.LocName,
            X = t.AxisX.GetValueOrDefault(),
            Y = t.AxisY.GetValueOrDefault(),
            Z = t.AxisZ.GetValueOrDefault(),
            T = t.AxisT.GetValueOrDefault(),
            nX = t.NAxisX.GetValueOrDefault(),
            nY = t.NAxisY.GetValueOrDefault(),
            nZ = t.NAxisZ.GetValueOrDefault(),
            nT = t.NAxisT.GetValueOrDefault(),
        }).OrderBy(t => t.LocType).ToList();
    }

    public int UpdateLocCoord(CoordPara coordPara)
    {
        var result = _RcsLocation.UpdateLocCoord(coordPara);
        return result;
    }

    public List<LocNameBean> GetLocNameBean()
    {
        var result = _RcsLocation.GetLocations().Where(f => f.LocType == (int)EnumLocType.Location).Select(f => new LocNameBean
        {
            Id = f.Id.GetValueOrDefault(),
            LocName = f.LocName
        }).ToList();
        return result;
    }
    public List<CrnEnableLocationBean> GetCrnEnableLocation()
    {
        var result = _RcsLocation.GetCrnEnableLocation();
        return result;
    }


    /// <summary>
    /// 同步MES
    /// </summary>
    /// <param name="skuId"></param>
    /// <param name="currentLoc"></param>
    //private void SyncMes(long skuId, long targetLoc)
    //{
    //    //入库完成（WMS->MES）堆垛机入库任务完成后，由WMS将信息推送至MES
    //    var tireBarcode = _ProdProduct.GetCodeBySkuId(skuId);
    //    _Wms2MesService.PushWarehousingFinish(tireBarcode, targetLoc.ToString());


    //    // 胎胚库存
    //    var location = _RcsLocation.GetLocationById(targetLoc);
    //    //var hold = _WcsEquipLocHold.GetEquipLocHoldByLoc(location?.Id ?? 0);
    //    //var crn = _RcsEquipCrn.GetCrnForkById(hold?.Id ?? 0);

    //    //var warehouseNo = (int)(crn?.WarehourseId ?? 0);
    //    var warehouseId = _RcsLocation.GetWarehouseByLocationID(targetLoc);
    //    int.TryParse(warehouseId.ToString(), out var warehouseNo);


    //    //_Rpc.SetTireStock(new()
    //    //{
    //    //    TirBarCode = tireBarcode,
    //    //    TireWarehouse = (int)EnumInventoryType.YES,
    //    //    WarehouseNo = warehouseNo,
    //    //    LocNo = location?.LocCode,
    //    //    LocXNo = location?.AxisT ?? 0, // 巷道
    //    //    LocYNo = location?.AxisY ?? 0, // 库位
    //    //    LocZNo = location?.AxisZ ?? 0, // 层
    //    //});
    //}

    private void SyncMesOnFinish(long skuId)
    {
        //推送出库订单状态(WMS->MES)
        List<PushOutOrderStatusParam> pushOutOrderStatusParams = new List<PushOutOrderStatusParam>();
        var tireBarcode = _ProdProduct.GetCodeBySkuId(skuId);
        //WriteLog($"执行完成异常出库11任务号{task.TaskNo}-sku{task.SkuId}-条码{tireBarcode},OrderDetailId{task.SummaryId?.ToString()}", true);
        pushOutOrderStatusParams.Add(new PushOutOrderStatusParam
        {
            OrderDetailId = "0",
            OrderStatus = (int)EnumPushOutOrderStatus.CrnTaskFinish,
            TireBarcode = tireBarcode
        });
        _Wms2MesService.PushOutOrderStatus(pushOutOrderStatusParams);

    }


    /// <summary>
    /// 同步MES
    /// </summary>
    /// <param name="skuId"></param>
    /// <param name="targetLoc"></param>
    private void SyncMes(long skuId)
    {
        //crn完成开始推送信息skuId-603892304247259637,summary605316946560287221,targetloc20060802
        WriteLog($"人工出库完成开始推送信息skuId-{skuId}", true);
        var tireBarcode = _ProdProduct.GetCodeBySkuId(skuId);
        //WriteLog($"ems完成开始推送信息tireBarcode-{tireBarcode}", true);
        //var curingRL = "N";// _RcsEquipCuring.GetCuringRLByCuringLocId(targetLoc);
        //var equipNo = curingRL?.CuringCode;
        var equipNo = "0";
        var model = "N";// curingRL?.Mode;
                        //var equipModel = _RcsEquipCuring.GetCuringRLByMesModelRL(model);
                        //var summmary = _WmsOutOrderSummary.GetOutOrderSummaryById(summaryId);

        var prodSku = _ProdSku.GetProdSkuById(skuId);
        var material = _ProdMaterial.GetMaterialById(prodSku?.MaterialId ?? 0);

        var mesService = K8.RSF.Client.Create<IMesService>();
        EndParam param = new EndParam()
        {
            TcmNo = equipNo,
            TcmDir = model,
            TcmSpec = material?.MaterialCode,
            TcmSpecDesc = material?.MaterialCode,
            TirCode = tireBarcode,
            Angle = 0,
            TbmPs = "0",
            CompFlag = "S",
            //TbmSpec=mesproduct?.TbmSpec
        };
        mesService.AddOutEnd(param);

    }

    public List<StockPairBean> GetStockPair()
    {
        var result = _RcsLocation.GetStockPair();
        return result;

    }

}
